#include "convolution.h"


Convolution::Convolution(QWidget *parent, PBO* pbo) : QWidget(parent)
{
	ui.setupUi(this);
	this->pbo = pbo;
	table = new QTableView(this);
	table->move(0,40);
	//QObject::connect(table->dat,SIGNAL(clicked()), this,SLOT(runn()));

	buttonOK = new QPushButton("OK", this);
	buttonOK->move(280,20);
	QObject::connect(buttonOK,SIGNAL(clicked()), this,SLOT(runn()));


	QSpinBox *sizeBox = new QSpinBox(this);
    sizeBox->setRange(3, 31);
    sizeBox->setSingleStep(2);
    sizeBox->setValue(3);
	sizee = 3;
	QObject::connect(sizeBox,SIGNAL(valueChanged(int)), this,SLOT(updateSize(int)));

	updateSize(3);
}

void Convolution::updateSize(int i) 
{
	this->sizee = i;
	//create model
    QStandardItemModel *model = new QStandardItemModel( sizee, sizee, this );
 
    //create QTableview Horizontal Header
    for (int r=0; r<sizee; r++)
	{
        model->setHorizontalHeaderItem( r, new QStandardItem( QString("%0" ).arg(r + 1)) );
	}
 
    //fill model value
    for( int r=0; r<sizee; r++ )
    {
        for( int c=0; c<sizee; c++)
        {
            QStandardItem *item = new QStandardItem(QString("1"));
            model->setItem(r, c, item);
        }
    }
	
    //set model
    table->setModel(model);
	convert(false);
	QObject::connect(model,SIGNAL(itemChanged(QStandardItem*)), this,SLOT(dataChanged(QStandardItem*)));
	 
	for( int i=0; i<sizee; i++ )table->setColumnWidth(i, 30);
}

void Convolution::dataChanged(QStandardItem* noMatter)
{
	qDebug("call: Convultion dataChanged()");
	convert(false);
}

void Convolution::runn()
{
	convert(true);
	this->close();
	this->deleteLater();
}

void Convolution::convert(bool pernam)
{
	QStandardItemModel* model = (QStandardItemModel*)table->model();
	float* data = new float[sizee*sizee];
	for(int r=0;r<sizee;r++) 
	{
		  for( int c=0; c<sizee; c++)
		  {
			  data[c + r * sizee] = model->item(r,c)->text().toFloat();
		  }
	}
	pbo->convolution(data, sizee, pernam);
}

Convolution::~Convolution()
{
	
}
